package oack1;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class SSLMail {
	public static void main(String[] args) throws AddressException, MessagingException {
		String host = "smtp.gmail.com";
	    String from = "akshit.nanda91@gmail.com";
	    String pass = "password";
	    String port = "587";
	    Properties props = System.getProperties();
	    props.put("mail.smtp.starttls.enable", "true"); // added this line
	    props.put("mail.smtp.host", host);
	    props.put("mail.smtp.user", from);
	    props.put("mail.smtp.password", pass);
	    props.put("mail.smtp.port", port);
	    props.put("mail.smtp.auth", "true");
	 
	    String[] to = {"rohit09039@iiitd.ac.in"}; // added this line
	 
	    Session session = Session.getDefaultInstance(props, null);
	    MimeMessage message = new MimeMessage(session);
	    message.setFrom(new InternetAddress(from));
	 
	    InternetAddress[] toAddress = new InternetAddress[to.length];
	 
	    // To get the array of addresses
	    for( int i=0; i < to.length; i++ ) { // changed from a while loop
	        toAddress[i] = new InternetAddress(to[i]);
	    }
	    System.out.println(Message.RecipientType.TO);
	 
	    for( int i=0; i < toAddress.length; i++) { // changed from a while loop
	        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
	    }
	    message.setSubject("sending in a group");
	    message.setText("Testing");
	    Transport transport = session.getTransport("smtp");
	    transport.connect(host, Integer.parseInt(port), from.toString().trim(), pass.toString().trim());
	    //transport.connect(smtp_host_name, smtp_host_port,smtp_username.toString().trim(),smtp_password.toString().trim());
	    transport.sendMessage(message, message.getAllRecipients());
	    transport.close();
}
}